package com.example.appmanager.ui.slideshow;

import android.content.Intent;
import android.os.Bundle;
import android.util.JsonReader;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.appmanager.Appconfig.Appconfig;
import com.example.appmanager.Appconfig.ToastShow;
import com.example.appmanager.MainActivity;
import com.example.appmanager.R;
import com.example.appmanager.entity.ReaponseMsg;
import com.example.appmanager.entity.UserTableEntity;
import com.example.appmanager.services.AllresposeService;
import com.example.appmanager.user.Login;


/*
这个是設置Fragment
* */
public class SlideshowFragment extends Fragment {

    private SlideshowViewModel slideshowViewModel;
    private TextView textAccount;// 姓名
    private TextView editName;//用户的账户号
    private EditText editPwd;//密码
    private EditText editAge;//年龄
    private boolean getData;
    private Button button;//这个是修改的按钮
    private RadioGroup radioGroup;//这个是用户的性别


    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        slideshowViewModel =
                ViewModelProviders.of(this).get(SlideshowViewModel.class);
        View root = inflater.inflate(R.layout.fragment_slideshow, container, false);
//        final TextView textView = root.findViewById(R.id.text_slideshow);
//        slideshowViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
//            @Override
//            public void onChanged(@Nullable String s) {
//                textView.setText(s);
//            }
//        });
        initUi(root);//这个是初始化界面的控件
        onClickListener();//这个是用户修改的按钮监听



        return root;
    }

    private void onClickListener() {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               setData();

            }
        });
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    //获取网络数据
    private boolean setData() {

        Log.d("SlideshowFragment",textAccount.getText().toString().trim());

        UserTableEntity userTableEntity = new UserTableEntity();
        userTableEntity.setAge(Integer.parseInt(editAge.getText().toString().trim()));
        userTableEntity.setUaccount(textAccount.getText().toString().trim());
        userTableEntity.setUname(editName.getText().toString().trim());
        userTableEntity.setUpassword(editPwd.getText().toString().trim());
        int count = radioGroup.getChildCount();
        for (int i = 0; i < count; i++) {
            RadioButton rb = (RadioButton) radioGroup.getChildAt(i);
            if (rb.isChecked()) {
                Log.d("userRegister", rb.getText().toString());
                userTableEntity.setSex(rb.getText().toString());//设置性别
                break;
            }
        }
        JSON json = (JSON) JSONObject.toJSON(userTableEntity);
        String str = json.toJSONString();
        Log.d("slideshowFragment",str);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Appconfig.BASE_URL)
                .client(Appconfig.CLIENT) //这是设置请求的时间等信息 连接时间  上传等时间
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        AllresposeService request = retrofit.create(AllresposeService.class);
        Call<ReaponseMsg> userEntityCall = request.updateUser(str);
        userEntityCall.enqueue(new Callback<ReaponseMsg>() {
            @Override
            public void onResponse(Call<ReaponseMsg> call, Response<ReaponseMsg> response) {
                /*
                 * */
                int id = response.body().getBug();
                Log.d("userRegister",String.valueOf(id));
                if (id==1){
                    getData = true;
                    Log.d("SlideshowFragment","成功！");
                    Intent intent = new Intent(getActivity(), Login.class);
                    startActivity(intent);
                    Toast toast = new ToastShow().getToast(getContext(),"修改成功Q！",1000);

                    Log.d("SlideshowFragment",String.valueOf(id));
                }

            }
            @Override
            public void onFailure(Call<ReaponseMsg> call, Throwable t) {
                 getData = false;
                Log.d("reasponseMsg",String.valueOf(t.getMessage()));
            }
        });
        Log.d("getData",String.valueOf(getData));
        return getData;



    }

    //初始化界面的控件
    private void initUi(View view){
        textAccount = view.findViewById(R.id.user_update_name);//用户的名字
        editName = view.findViewById(R.id.user_update_account);//用户的账户
        editPwd = view.findViewById(R.id.user_update_pwd);//密码
        editAge = view.findViewById(R.id.user_update_age);//年龄
        button = view.findViewById(R.id.user_update_save);
        radioGroup = view.findViewById(R.id.update_userSex);

        String str = getActivity().getIntent().getStringExtra("userEntity");

        UserTableEntity userTableEntity = JSONObject.parseObject(str,UserTableEntity.class);
        textAccount.setText(userTableEntity.getUaccount());
        editName.setText(userTableEntity.getUname());
        editAge.setText(String.valueOf(userTableEntity.getAge()));
        editPwd.setText(userTableEntity.getUpassword());
       // Log.d("MainActivity", uName + "----->" + uPwd);
        Log.d("MainActivity009", str );
        //UserTableEntity

    }
}
